package com.douqu.game.core.config;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.Utils;
import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.config.drop.DropObject;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;


/**
 * @author: Gavin.
 * Description: 十连抽
 * Date: 2017/8/18 0018 下午 3:47
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class LotteryConfig extends GameObject {

    public int type;   //类型
    public int level;  //级别
    public int freeCount;   //免费次数
    public int cdTime;     //cd时间单位（秒）
    public DropObject[] props; //道具掉落配置
    public DropObject[] assets; //资源掉落配置
    public DropObject[] cards; //卡片掉落配置
    public GoodsData reward; //必送奖励
    public GoodsData priorityUse;//消耗道具
    public GoodsData needAsset; //资源消耗
    public int nextLevelCount; //到下一级的次数
    public int nextLevelPond;  //下一级奖池

    /**
     * 总的权重
     */
    private int totalOdds;


    @Override
    public boolean check()
    {
        if(props == null)
        {
            LogUtils.error("LotteryObject check props is null -> id:" + id + " name:" + name);
            return false;
        }

        if(assets == null)
        {
            LogUtils.error("LotteryObject check assets is null -> id:" + id + " name:" + name);
            return false;
        }

        if(cards == null)
        {
            LogUtils.error("LotteryObject check cards is null -> id:" + id + " name:" + name);
            return false;
        }

        if(priorityUse == null){
            LogUtils.error("LotteryObject check priorityUse is null -> id:" + id +" name:" + name);
            return false;
        }

        if(needAsset == null)
        {
            LogUtils.error("LotteryObject check needAsset is null -> id:" + id + " name:" + name);
            return false;
        }
        if(reward == null)
        {
            LogUtils.error("LotteryObject check reward is null -> id:" + id + " name:" + name);
            return false;
        }

        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.LOTTERY_KEY, nextLevelPond) == null)
        {
            LogUtils.error("LotteryObject check nextLevelPond is null -> id:" + id + " name:" + name);
            return false;
        }

        totalOdds = 0;
        for(int i = 0 ; i < cards.length; i++){
            totalOdds += cards[i].odds;
        }
        for(int i = 0 ; i < assets.length; i++){
            totalOdds += assets[i].odds;
        }
        for(int i = 0 ; i < props.length; i++){
            totalOdds += props[i].odds;
        }

        LogUtils.info("抽奖配置->id:", id, ", needAsset:", needAsset);
        return true;
    }


//    /***
//     * 获取道具
//     * @return
//     */
//    public DropObject getProp(){
//        //道具掉落类型如果是随机
//            int c = 0;
//            for(int i = 0 ; i < props.length; i++){
//                c += props[i].odds;
//            }
//            int r = new Random(c).nextInt();
//            int temp = 0;
//            for (int j = 0 ; j < props.length; j++){
//                temp += props[j].odds;
//                if(r <= temp){
//                    return props[j];
//                }
//            }
//        return null;
//    }
//
//
//    /***
//     * 获取资源
//     * @return
//     */
//    public DropObject getAsset(){
//        //道具掉落类型如果是随机
//        int c = 0;
//        for(int i = 0 ; i < assets.length; i++){
//            c += assets[i].odds;
//        }
//        int r = new Random(c).nextInt();
//        int temp = 0;
//        for (int j = 0 ; j < assets.length; j++){
//            temp += assets[j].odds;
//            if(r <= temp){
//                return assets[j];
//            }
//        }
//        return null;
//    }
//
//    /***
//     * 获取卡片
//     * @return
//     */
//    public DropObject getCard(){
//        //道具掉落类型如果是随机
//        int c = 0;
//        for(int i = 0 ; i < cards.length; i++){
//            c += cards[i].odds;
//        }
//        int r = new Random(c).nextInt();
//        int temp = 0;
//        for (int j = 0 ; j < cards.length; j++){
//            temp += cards[j].odds;
//            if(r <= temp){
//                return cards[j];
//            }
//        }
//        return null;
//    }

    /***
     * 根据权重值来计算走哪一个掉落库
     * @return
     */
    public GoodsData getDrop() {
        if(totalOdds > 0){
            int r = Utils.getRandom(totalOdds);
            //LogUtils.debug("r------------------>" + r);
            int temp = 0;
            for (int j = 0 ; j < cards.length; j++){
                temp += cards[j].odds;
                if(r <= temp){
                    return new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE, cards[j].id, cards[j].count);
                }
            }
            for (int j = 0 ; j < assets.length; j++){
                temp += assets[j].odds;
                if(r <= temp){
                    return new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE, assets[j].id, assets[j].count);
                }
            }
            for (int j = 0 ; j < props.length; j++){
                temp += props[j].odds;
                if(r <= temp){
                    return new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE, props[j].id, props[j].count);
                }
            }
        }
        return null;
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("props".equals(key))
        {
            props = LoadUtils.loadDropObject(value);
            return LoadUtils.checkDropObject(props, this.getClass(), key, DataFactory.PROP_KEY, id);
        }
        else if("assets".equals(key))
        {
            assets = LoadUtils.loadDropObject(value);
            return LoadUtils.checkDropObject(assets, this.getClass(), key, DataFactory.ASSET_KEY, id);
        }
        else if("cards".equals(key))
        {
            cards = LoadUtils.loadDropObject(value);
            return LoadUtils.checkDropObject(cards, this.getClass(), key, DataFactory.CARD_KEY, id);
        }
        else if("priorityUse".equals(key))
        {
            priorityUse = new GoodsData(value);
            if(priorityUse.type == 0 && priorityUse.id == 0 && priorityUse.value == 0){
                return true;
            }
            return priorityUse.checkProp(this.getClass(),key,id);
        }
        else if("needAsset".equals(key))
        {
            needAsset = new GoodsData(value);
            return needAsset.checkAsset(this.getClass(), key, id);
        }
        else if("reward".equals(key))
        {
            reward = new GoodsData(value);
            return reward.checkProp(this.getClass(), key, id);
        }
        else
            return super.setVariable(key, value);
    }






}
